<?php
class Login extends CI_Controller {
    var $data = array();
    function __construct(){
		parent::__construct();
		$this->load->model('M_user');
        $this->load->helper(array('url','captcha','form'));
		$this->load->library('form_validation');
	}
    
    function index(){
		if ($this->M_user->is_logged_in()){
			redirect('mainpage'); 
		}
		$this->data['yzm'] = $this->M_user->getcheckword();
		$this->data['login_error'] = "";
		$this->form_validation->set_rules('username','用户名','required',array('required'=>'用户名不能为空!'));
		$this->form_validation->set_rules('password','密码','required',array('required'=>'密码不能为空!'));
		$this->form_validation->set_rules('checknum','验证码','required',array('required'=>'验证码不能为空!'));
		if($this->form_validation->run()) {
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$checknum = $this->input->post('checknum');
			if($this->M_user->get_check_word($checknum,$this->input->ip_address(),time()-1000)){
				if ($user = $this->M_user->get_by_username($username)) {	
					if ($this->M_user->check_password($password,$user['password'])){
						$this->M_user->allow_pass($user);
						redirect('mainpage');
					} 
					else { 	
						$this->data['login_error'] = '登录失败:无效的用户名或密码!'; 
					}
				} 
				else{
					$this->data['login_error'] = '登录失败:无效的用户名或密码!';
				}
			}else{
				$this->data['login_error'] = '验证码错误!';
			}
		}
		$this->load->view('loginview',$this->data);
    }
	
	function register(){
		if ($this->M_user->is_logged_in()){
			redirect('mainpage'); 
		}
		$this->load->view('registerview');
	}
	
	function findpwd(){
		if($this->M_user->is_logged_in()){
			redirect('mainpage');
		}
		$this->load->view('findwordview');
	}
	
	function getuserinfo(){
		
		$qst = $_POST['question'];
		$ans = $_POST['answer'];
		
		if($qst == '' && $ans != ''){
			echo "请输入密保问题";
		}
		if($ans == '' && $qst != ''){
			echo "请输入密保答案";
		}
		if($ans == '' && $qst == ''){
			echo "请输入密保问题和答案";
		}
		if($ans != '' && $qst != ''){
			$userinfo = array('username'=>$_POST['username'],
							  'password'=>md5($_POST['password']),
							  'email'=>$_POST['email'],
							  'question'=>$_POST['question'],
							  'answer'=>$_POST['answer']);
			$res = $this->M_user->insert_user_data($userinfo);
			if($res){
				echo "注册成功!";
			}else{
				echo "注册失败!";
			}
		}
		
	}
	
	function findword(){
		$username = $_POST['username'];
		if($username == ''){
			echo $info = '{"tips":"请输入用户名"}';
		}else{
			$res = $this->M_user->findquestion($username);
			if($res){
				$question = $res['question'];
				echo $info = '{"tips":"","question":"'.$question.'"}';
			}else{
				echo $info = '{"tips":"该用户不存在"}';
			}
		}
	}
	
	function checkanswer(){
		$username = $_POST['username'];
		$question = $_POST['question'];
		$answer = $_POST['answer'];
		if($username == '' or $question == '' or $answer == ''){
			echo '{"info":"用户信息不完整"}';
		}else{
			$res = $this->M_user->resetpwd($username,$question,$answer);
			if($res){
				echo '{"info":"密码重置成功"}';
			}else{
				echo '{"info":"用户验证失败"}';
			}
		}
	}
	
	function checkword(){
		$res = $this->M_user->getcheckword();
		echo '&nbsp;&nbsp;&nbsp;'.$res;
	} 
	
}

?>